﻿using System;
using System.Runtime.Serialization;

namespace RockBus.DataContracts
{
    [DataContract(Namespace = StringConstants.RockBusNamespace)]
    public class FilterInfo
    {
        public FilterInfo()
        {
        }

        public FilterInfo(string id, FilterTypeEnum filterType, string filterData, string customTypeName = null)
            : this()
        {
            this.Id = id;
            this.FilterType = filterType;
            this.FilterData = filterData;

            if ((customTypeName != null) &&
                (filterType != FilterTypeEnum.Custom))
            {
                throw new ArgumentException("Shouldn't provide a customTypeName when filterType is not Custom");
            }

            if ((filterType == FilterTypeEnum.Custom) &&
                (string.IsNullOrEmpty(customTypeName)))
            {
                throw new ArgumentException("Should provide a customTypeName when filterType is Custom");
            }

            if (!string.IsNullOrEmpty(customTypeName))
            {
                this.CustomTypeName = customTypeName;
            }
        }

        public FilterInfo(string id, string filter1Id, string filter2Id, string filterData = null)
            : this(id, FilterTypeEnum.And, filterData)
        {
            this.Filter1Id = filter1Id;
            this.Filter2Id = filter2Id;
        }

        [DataMember]
        public string Id { get; set; }

        [DataMember]
        public FilterTypeEnum FilterType { get; set; }

        [DataMember]
        public string CustomTypeName { get; set; }

        [DataMember]
        public string Filter1Id { get; set; }

        [DataMember]
        public string Filter2Id { get; set; }

        [DataMember]
        public string FilterData { get; set; }

        public bool HasNoFilterReference
        {
            get
            {
                return (string.IsNullOrEmpty(this.Filter1Id) && string.IsNullOrEmpty(this.Filter2Id));
            }
        }

        public bool HasOneFilterReference
        {
            get
            {
                return (!string.IsNullOrEmpty(this.Filter1Id) || !string.IsNullOrEmpty(this.Filter2Id));
            }
        }

        public bool HasTwoFilterReference
        {
            get
            {
                return (!string.IsNullOrEmpty(this.Filter1Id) && !string.IsNullOrEmpty(this.Filter2Id));
            }
        }

        //private static char seperator = '/';
        //private static string seperators = ".,\\:;_|";
        //private static string omitchars = "* ?";
        public void Normalize()
        {
            switch (this.FilterType)
            {
                case FilterTypeEnum.Action:
                    break;

                case FilterTypeEnum.EndpointAddress:
                    break;

                case FilterTypeEnum.PrefixEndpointAddress:
                    break;

                case FilterTypeEnum.And:
                    break;

                case FilterTypeEnum.Custom:
                    break;

                case FilterTypeEnum.EndpointName:
                    break;

                case FilterTypeEnum.MatchAll:
                    break;

                case FilterTypeEnum.XPath:
                    break;

                case FilterTypeEnum.Topic:
                    //this.FilterData = this.FilterData.ToLower();
                    //StringBuilder newFilterDataSb = new StringBuilder();
                    //foreach (char c in this.FilterData)
                    //{
                    //    if (!omitchars.Contains(c))
                    //    {
                    //        if (seperators.Contains(c))
                    //        {
                    //            newFilterDataSb.Append(seperator);
                    //        }
                    //        else
                    //        {
                    //            newFilterDataSb.Append(c);
                    //        }
                    //    }
                    //}
                    break;

                default:
                    break;
            }
        }
    }
}